package com.saifan.wyy_ov.ui.onlishop;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.CartBean.GoodsList;
import com.saifan.wyy_ov.utils.PriceUtils;

public class AdapterSubmitItem extends BaseAdapter {
    List<GoodsList> goodsLists;
    Context context;
    Handler handler;
    static int CHANGETOTAL = 12;

    public AdapterSubmitItem(List<GoodsList> goodsLists, Context context, Handler handler) {
        
        this.goodsLists = goodsLists;
        this.context = context;
        this.handler = handler;
    }

    @Override
    public int getCount() {
         
        if ( goodsLists!=null||goodsLists.isEmpty() ) {
            return 0;
        } else {
            return goodsLists.size();
        }
    }

    @Override
    public Object getItem(int paramInt) {
        // 
        return goodsLists.get(paramInt);
    }

    @Override
    public long getItemId(int paramInt) {

        return 0;
    }

    @Override
    public View getView(int paramInt, View convertView, ViewGroup paramViewGroup) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder(goodsLists.get(paramInt), context, handler);
            convertView = viewHolder.convertView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    static class ViewHolder implements OnClickListener, TextWatcher {
        GoodsList goodsList;
        Context context;
        View convertView;
        Handler handler;

        public ViewHolder(GoodsList goodsList, Context context, Handler handler) {

            this.goodsList = goodsList;
            this.context = context;
            this.handler = handler;
            convertView = LayoutInflater.from(context).inflate(R.layout.order_item_view, null);
            tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            tv_total = (TextView) convertView.findViewById(R.id.tv_total);
            ed_remark = (EditText) convertView.findViewById(R.id.ed_remark);
            bt_add = (Button) convertView.findViewById(R.id.bt_add);
            bt_sub = (Button) convertView.findViewById(R.id.bt_sub);
            tv_count = (TextView) convertView.findViewById(R.id.tv_count);
            bt_add.setOnClickListener(this);
            bt_sub.setOnClickListener(this);
            tv_goods_name.setText(goodsList.getGoodsName());
            tv_count.setText(goodsList.getCount() + "");
            tv_price.setText(goodsList.getGoodsPrice() + "");
            tv_total.setText(PriceUtils.getPrice(goodsList.getCount() * goodsList.getGoodsPrice()) + "");
            ed_remark.addTextChangedListener(this);
        }

        public TextView tv_goods_name;
        public TextView tv_price;
        public TextView tv_total, tv_count;
        public EditText ed_remark;
        public Button bt_add, bt_sub;

        @Override
        public void onClick(View arg0) {

            if (arg0.getId() == R.id.bt_add) {
                goodsList.setCount(goodsList.getCount() + 1);
                tv_count.setText(goodsList.getCount() + "");
                tv_total.setText(PriceUtils.getPrice(goodsList.getCount() * goodsList.getGoodsPrice()) + "");
                Message message = new Message();
                message.what = CHANGETOTAL;
                message.obj = goodsList.getGoodsPrice();
                handler.sendMessage(message);

            }
            if (arg0.getId() == R.id.bt_sub) {
                if (goodsList.getCount() > 1) {
                    goodsList.setCount(goodsList.getCount() - 1);
                    tv_count.setText(goodsList.getCount() + "");
                    tv_total.setText(PriceUtils.getPrice(goodsList.getCount() * goodsList.getGoodsPrice()) + "");
                    Message message = new Message();
                    message.what = CHANGETOTAL;
                    message.obj = -goodsList.getGoodsPrice();
                    handler.sendMessage(message);
                }
            }
        }

        @Override
        public void afterTextChanged(Editable arg0) {

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            goodsList.setNote(arg0.toString());
        }
    }
}